package com.yanqu.road.dao.impl.beautyescort;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.beautyescort.rank.EscortWeekUnionValue;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EscortWeekUnionValueDaoImpl extends TempDataObjectDao<EscortWeekUnionValue> {

    public static String getEscortWeekUnionValueSql = "select * from `t_u_escort_week_union_value` where `weekId`=? or `weekId`=?";
    public Map<Integer, Map<String, EscortWeekUnionValue>> getEscortWeekUnionValue(int preWeekId, int thisWeekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<String, EscortWeekUnionValue>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getEscortWeekUnionValueSql);
            int i = 1;
            call.setInt(i++, preWeekId);
            call.setInt(i++, thisWeekId);
            rs = call.executeQuery();
            while (rs.next()) {
                EscortWeekUnionValue data = parse(rs);
                if(!result.containsKey(data.getWeekId())){
                    result.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                result.get(data.getWeekId()).put(data.getUnionUid(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertEscortWeekUnionValueSql = "insert into `t_u_escort_week_union_value`(weekId,unionUid,unionBaseInfo,userList,value) values (?,?,?,?,?)";
    @Override
    protected boolean addData(EscortWeekUnionValue data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getUserList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        if (getDb().executeNoneQuery(insertEscortWeekUnionValueSql, para) > 0) {
            return true;
        }
        return false;
    }


    public static String updateEscortWeekUnionValueSql = "update `t_u_escort_week_union_value` set unionBaseInfo=?,userList=?,value=? where weekId=? and unionUid=?";
    @Override
    protected boolean updateData(EscortWeekUnionValue data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnionBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getUserList(), ",")));
        para.put(i++, new DbParameter(Types.BIGINT, data.getValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getUnionUid()));
        if (getDb().executeNoneQuery(updateEscortWeekUnionValueSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected EscortWeekUnionValue parseData(ResultSet rs) throws SQLException {
        EscortWeekUnionValue data = new EscortWeekUnionValue();
        data.setWeekId(rs.getInt("weekId"));
        data.setUnionUid(rs.getString("unionUid"));
        data.setUnionBaseInfo(JSON.parseObject(rs.getString("unionBaseInfo"), UnionBaseInfo.class));
        data.setUserList(StringUtils.stringToLongList(rs.getString("userList"), ","));
        data.setValue(rs.getLong("value"));

        return data;
    }
}

